import * as company from '../api/company';
import { message } from 'antd/lib/index';
import checkStatus from '@/utils/status';

export default {
  namespace: 'companyModels',

  state: {
    data: {
      data: [],
      pagination: {},
    },
  },
  effects: {
    //请求列表
    * fetch({ payload }, { call, put }) {
      const response = yield call(company.EnterpriseList, payload);
      yield checkStatus(response);
      yield put({
        type: 'get',
        payload: response.msg,
      });
    },
    * remove({ payload, callback }, { call, put }) {
      const response = yield call(company.enterpriseDel, payload);
      yield checkStatus(response);

      yield put({
        type: 'enterpriseDel',
        payload: response.msg,
      });
      if (callback) callback(response);
    },
    * getEnterpriseDesc({ payload, callback }, { call, put }) {

      const response = yield call(company.enterpriseDetail, payload);
      localStorage.setItem('enterpriseDetail', JSON.stringify(response.msg));
      yield checkStatus(response);

      yield put({
        type: 'enterpriseDesc',
        payload: response.msg,
      });
      if (callback) callback(response);

    },
    * audit({ payload, callback }, { call, put }) {
      const response = yield call(company.enterpriseAudit, payload);
      yield checkStatus(response);

      yield put({
        type: 'enterpriseAudit',
        payload: response.msg,
      });
      if (callback) callback(response);
    },
    * accountOpen({ payload, callback }, { call, put }) {
      const response = yield call(company.accountOpen, payload);
      yield checkStatus(response);
      if (callback) callback(response);
    },
  },

  reducers: {
    get(state, action) {
      console.log('action.payload', action.payload);
      state.pagination = {
        pageSize: action.payload.pageSize,
        current: action.payload.pageNo,
        total: action.payload.total,
      };
      return {
        ...state,
        data: action.payload,
      };
    },
    enterpriseDesc(state, action) {
      return {
        ...state,
        companyDesc: action.payload,
      };
    },
    enterpriseDel(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    enterpriseAudit(state, action) {
      return {
        ...state,
        companyDesc: action.payload,
      };
    },

  },
};
